package com.lmpop.trade.ti_user;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.MD5;
import com.lmpop.trade.ti_company.Ti_companyInfo;
import com.lmpop.trade.ti_personal.Ti_personalInfo;

public class Ti_userInfo implements TradeInterf
{
  QueryList queryList;

  public Ti_userInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    String userrand = (String)map.getObject("userrand");
    String randcode = "";
    if (userrand != null) {
      userrand = map.getString("userrand");
      randcode = map.getString("randcode");
    }
    ti_user.setUser_id(map.getString("user_id"));
    ti_user.setUser_name(map.getString("user_name"));
    ti_user.setUser_type(map.getString("user_type"));
    ti_user.setCust_id(map.getString("cust_id"));

    MD5 md5 = new MD5();
    String passwd = map.getString("passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());

    ti_user.setPasswd(passwdmd5);

    ti_user.setUser_state(map.getString("user_state"));
    if (map.getString("passwd_ques") != null) {
      ti_user.setPasswd_ques(map.getString("passwd_ques"));
      ti_user.setPasswd_answer(map.getString("passwd_answer"));
    } else {
      ti_user.setPasswd_ques("");
      ti_user.setPasswd_answer("");
    }

    ti_user.setReal_name(map.getString("real_name"));
    ti_user.setOrg_id(map.getString("org_id"));
    ti_user.setRole_code(map.getString("role_code"));
    if (map.containsKey(map.getString("last_login")))
      ti_user.setLast_login(map.getString("last_login"));
    else
      ti_user.setLast_login("");
    ti_user.setPub_user_id(map.getString("pub_user_id"));
    ti_user.setRemark(map.getString("remark"));

    String result_code = "0";
    String result_info = "";
    try {
      if ((userrand != null) && (!userrand.equals(""))) {
        if (userrand.trim().equals(randcode.trim())) {
          insert(dbmMgr, ti_user);

          setUserInfos(map);
        } else {
          result_info = "验证码输入不正确!";
          result_code = "1";
        }
      } else {
        insert(dbmMgr, ti_user);

        setUserInfos(map);
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void setUserInfos(SysMap map)
  {
    String user_id = map.getString("user_id");
    String cust_id = map.getString("cust_id");
    String user_type = map.getString("user_type");
    String role_code = map.getString("role_code");
    String user_name = map.getString("user_name");
    String cust_class = map.getString("cust_class");

    map.setSessionField("session_user_id", user_id);
    map.setSessionField("session_cust_id", cust_id);
    map.setSessionField("session_user_type", user_type);
    map.setSessionField("session_role_code", role_code);
    map.setSessionField("session_user_name", user_name);
    map.setSessionField("session_cust_class", cust_class);

    map.setString("session_user_id", user_id);
    map.setString("session_cust_id", cust_id);
    map.setString("session_user_type", user_type);
    map.setString("session_role_code", role_code);
    map.setString("session_user_name", user_name);
    map.setString("session_cust_class", cust_class);
  }

  public void insert(DbmInterf dbmMgr, Ti_user ti_user)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_user", ti_user);
  }

  public SysMap deleteByCustID(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; deleteByCustID(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void deleteByCustID(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_user where cust_id=#value#
    dbmMgr.update("deleteTi_userByCustID", pkid);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_user", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    ti_user.setUser_id(map.getString("user_id"));
    ti_user.setUser_name(map.getString("user_name"));
    ti_user.setCust_id(map.getString("cust_id"));
    ti_user.setUser_type(map.getString("user_type"));

    ti_user.setUser_state(map.getString("user_state"));
    ti_user.setPasswd_ques(map.getString("passwd_ques"));
    ti_user.setPasswd_answer(map.getString("passwd_answer"));
    ti_user.setReal_name(map.getString("real_name"));
    ti_user.setOrg_id(map.getString("org_id"));
    ti_user.setRole_code(map.getString("role_code"));
    ti_user.setLast_login(map.getString("last_login"));
    ti_user.setPub_user_id(map.getString("pub_user_id"));
    ti_user.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_user);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_user ti_user)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_user", ti_user);
  }

  public SysMap updateUserState(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    String pkid_string = map.getString("pkid");
    String user_state = map.getString("user_state");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (!pkStr[i].equals(""))
            ti_user.setUser_state(map.getString("user_state"));
          ti_user.setUser_id(pkStr[i]);
          updateUserState(dbmMgr, ti_user);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateUserState(DbmInterf dbmMgr, Ti_user ti_user) throws ApplicationException {
    dbmMgr.update("updateTi_userstate", ti_user);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Map ti_user = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_user.put("start", Integer.valueOf(start));
    ti_user.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_userPage", ti_user);
  }

  public int getCountByObj(Object obj)
  {
    Map ti_user = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTi_user", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_userByPk", pk);
  }

  public SysMap updatePassWord(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    ti_user.setUser_id(map.getString("user_id"));
    ti_user.setCust_id(map.getString("cust_id"));
    MD5 md5 = new MD5();
    String passwd = map.getString("passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());
    ti_user.setPasswd(passwdmd5);

    String result_code = "0";
    try {
      updatePassWord(dbmMgr, ti_user);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatePassWord(DbmInterf dbmMgr, Ti_user ti_user)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_userPassWord", ti_user);
  }

  public String getSelectString(Object obj, String user_name_params)
  {
    Map ti_user = (Hashtable)obj;
    List list = this.queryList.getList("getListByTi_userPage", ti_user);
    String selectString = "";
    String user_id = "";
    String user_name = "";
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable newsMap = (Hashtable)list.get(i);
        if ((newsMap.get("user_id") != null) && (!newsMap.get("user_id").toString().equals(""))) {
          user_id = newsMap.get("user_id").toString();
        }
        if ((newsMap.get("user_name") != null) && (!newsMap.get("user_name").toString().equals(""))) {
          user_name = newsMap.get("user_name").toString();
        }
        if (user_name.equals(user_name_params))
          selectString = selectString + "<option value='" + user_id + "'selected>" + user_name + "</option>";
        else {
          selectString = selectString + "<option value='" + user_id + "'>" + user_name + "</option>";
        }
      }
    }
    return selectString;
  }

  public SysMap checkLogin(DbmInterf dbmMgr, SysMap map)
  {
    String user_name = map.getString("user_name");
    String passwd = map.getString("passwd");
    String bpm_id = map.getString("bpm_id");
    String userrand = "";
    if (map.getString("userrand") != null)
      userrand = map.getString("userrand");
    String randcode = "";
    if (map.getString("randcode") != null)
      randcode = map.getString("randcode");
    String result_code = "0";
    String passwdmd5 = passwd;
    String md5passwd = map.getString("md5passwd");
    if ((md5passwd == null) || ("".equals(md5passwd))) {
      MD5 md5 = new MD5();
      passwdmd5 = md5.getMD5(passwd.getBytes());
    }

    String result_info = "";
    String check_code = "0";
    List userList = checkUser(user_name, passwdmd5);
    if ((userList != null) && (userList.size() > 0)) {
      if ((userrand.equals("")) || (userrand.trim().equals(randcode.trim()))) {
        Hashtable userMap = (Hashtable)userList.get(0);
        String user_id = ""; String cust_type = ""; String cust_id = ""; String user_state = ""; String user_type = ""; String role_code = ""; String cust_class = ""; String area_attr = "";
        if (userMap.get("user_id") != null) user_id = userMap.get("user_id").toString();
        if (userMap.get("cust_type") != null) cust_type = userMap.get("cust_type").toString();
        if (userMap.get("cust_class") != null) cust_class = userMap.get("cust_class").toString();
        if (userMap.get("cust_id") != null) cust_id = userMap.get("cust_id").toString();
        if (userMap.get("state_code") != null) user_state = userMap.get("state_code").toString();
        if (userMap.get("user_type") != null) user_type = userMap.get("user_type").toString();
        if (userMap.get("role_code") != null) role_code = userMap.get("role_code").toString();
        if (cust_type.equals("1"))
        {
          Ti_personalInfo personalInfo = new Ti_personalInfo();
          List custlist = personalInfo.getListByPk(cust_id);
          if ((custlist != null) && (custlist.size() > 0))
          {
            Hashtable custMap = (Hashtable)custlist.get(0);
            if (custMap.get("area_attr") != null) area_attr = custMap.get("area_attr").toString(); 
          }
        }
        else if (cust_type.equals("0")) {
          Ti_companyInfo companyInfo = new Ti_companyInfo();
          List custlist = companyInfo.getListByPk(cust_id);
          if ((custlist != null) && (custlist.size() > 0))
          {
            Hashtable custMap = (Hashtable)custlist.get(0);
            if (custMap.get("area_attr") != null) area_attr = custMap.get("area_attr").toString();
          }
        }

        if (user_state.equals("d")) {
          result_info = "对不起，该用户已禁用，不允许登录!";
          result_code = "1";
          check_code = "1";
        } else if ((!cust_type.equals("0")) && (bpm_id.equals("2627"))) {
          result_info = "对不起，非企业用户禁止登录！";
          result_code = "1";
          check_code = "1";
        } else if (user_state.equals("b")) {
          result_info = "对不起，该用户审核未通过";
          result_code = "1";
          check_code = "1";
        } else {
          map.setSessionField("session_user_id", user_id);
          if ((area_attr == null) || ("".equals(area_attr))) {
            area_attr = "s8816448NNG5I3X";
          }
          map.setSessionField("session_area_attr", area_attr);
          map.setSessionField("session_cust_type", cust_type);
          map.setSessionField("session_cust_class", cust_class);
          map.setSessionField("session_cust_id", cust_id);
          map.setSessionField("session_user_type", user_type);
          map.setSessionField("session_role_code", role_code);
          map.setSessionField("session_user_name", user_name);
          map.setSessionField("session_check_code", check_code);
          map.setString("session_user_id", user_id);
          map.setString("session_area_attr", area_attr);
          map.setString("session_cust_type", cust_type);
          map.setString("session_cust_class", cust_class);
          map.setString("session_cust_id", cust_id);
          map.setString("session_user_type", user_type);
          map.setString("session_role_code", role_code);
          map.setString("session_user_name", user_name);
          if (cust_type.equals("0")) {
            map.setString("jumpurl", "/company_member.html");
          }
          try
          {
            updateLastUserLoginTime(dbmMgr, user_id);
          } catch (ApplicationException e) {
            e.printStackTrace();
          }
        }
      } else {
        result_info = "验证码输入不正确!";
        result_code = "1";
        check_code = "1";
      }
    } else {
      result_info = "用户名不存在或密码错误!";
      result_code = "1";
      check_code = "1";
    }
    map.setString("result_code", result_code);
    map.setString("result_info", result_info);
    return map;
  }

  public void updateLastUserLoginTime(DbmInterf dbmMgr, String user_id)
    throws ApplicationException
  {
    dbmMgr.update("updateLastUserLoginTime", user_id);
  }

  public List checkUser(String user_name, String passwd) {
    Map ti_user = new Hashtable();
    ti_user.put("user_name", user_name);
    ti_user.put("passwd", passwd);
    return this.queryList.getList("checkUserLoginPoint", ti_user);
  }

  public int checkuserName(String user_name)
  {
    List user_list = this.queryList.getList("checkuserName", user_name);
    if ((user_list != null) && (user_list.size() > 0)) {
      return 1;
    }
    return 0;
  }

  public int checkpasswdAnswer(String user_id, String passwd_answer)
  {
    Map map = new Hashtable();
    map.put("user_id", user_id);
    map.put("old_passwd", passwd_answer);
    List user_list = this.queryList.getList("checkpasswdAnswer", map);
    if ((user_list != null) && (user_list.size() > 0)) {
      return 1;
    }
    return 0;
  }

  public int checkOldPW(String user_id, String old_passwd)
  {
    MD5 md5 = new MD5();
    String passwdmd5 = md5.getMD5(old_passwd.getBytes());
    Map map = new Hashtable();
    map.put("user_id", user_id);
    map.put("old_passwd", passwdmd5);
    List user_list = this.queryList.getList("checkpasswdAnswer", map);
    if ((user_list != null) && (user_list.size() > 0)) {
      return 1;
    }
    return 0;
  }

  public SysMap updateBasic(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    ti_user.setUser_id(map.getString("user_id"));
    ti_user.setReal_name(map.getString("real_name"));
    ti_user.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      updateBasic(dbmMgr, ti_user);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateBasic(DbmInterf dbmMgr, Ti_user ti_user)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_userBasic", ti_user);
  }

  public SysMap updateuserPWQ(DbmInterf dbmMgr, SysMap map)
  {
    Ti_user ti_user = new Ti_user();
    ti_user.setUser_id(map.getString("user_id"));
    ti_user.setPasswd_ques(map.getString("passwd_ques"));
    ti_user.setPasswd_answer(map.getString("passwd_answer"));

    String result_code = "0";
    try {
      updateuserPWQ(dbmMgr, ti_user);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateuserPWQ(DbmInterf dbmMgr, Ti_user ti_user)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_userPWQ", ti_user);
  }

  public List getpwdinfo(String name)
  {
    return this.queryList.getList("getpwdinfo", name);
  }

  public List getUserNameByPK(String id)
  {
    return this.queryList.getList("getUserNameByPk", id);
  }

  public String getPersonalNameByUserId(String user_id)
  {
    List custList = this.queryList.getList("get_PersonalName_By_UserId", user_id);
    String user_name = "";
    Hashtable custMap = new Hashtable();
    if ((custList != null) && (custList.size() > 0)) custMap = (Hashtable)custList.get(0);
    if (custMap.get("user_name") != null) user_name = custMap.get("user_name").toString();
    return user_name;
  }

  public String getCustIDByName(String user_id) {
    List custList = this.queryList.getList("getCustIDByName", user_id);
    String cust_id = "";
    Hashtable custMap = new Hashtable();
    if ((custList != null) && (custList.size() > 0)) custMap = (Hashtable)custList.get(0);
    if (custMap.get("cust_id") != null) cust_id = custMap.get("cust_id").toString();
    return cust_id;
  }
  public String getCustClassByCustID(String user_id) {
    List custList = this.queryList.getList("getCustClassByCustID", user_id);
    String cust_class = "";
    Hashtable custMap = new Hashtable();
    if ((custList != null) && (custList.size() > 0)) custMap = (Hashtable)custList.get(0);
    if (custMap.get("cust_class") != null) cust_class = custMap.get("cust_class").toString();
    return cust_class;
  }

  public String getUserIdByCustId(String cust_id) {
    List list = this.queryList.getList("getUserIdByCustId", cust_id);
    if ((list != null) && (list.size() > 0)) {
      return ((Hashtable)list.get(0)).get("user_id").toString();
    }
    return null;
  }

  public List getComCodeByUserID(String user_id) {
    return this.queryList.getList("getComCodeByUserID", user_id);
  }

  public List getPersonalPwdByPwdQuestion(Map map) {
    return this.queryList.getList("getPersonalPwdByPwdQuestion", map);
  }
}